package com.lw.extraceim.ui.fragments;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.google.gson.reflect.TypeToken;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.lw.extraceim.R;
import com.lw.extraceim.app.ExtraceApplication;
import com.lw.extraceim.loader.PackageLoader;
import com.lw.extraceim.model.TransPackage;
import com.lw.extraceim.net.HttpResponseParam;
import com.lw.extraceim.net.ResponseCode;
import com.lw.extraceim.net.callback.HttpResponseParamCallback;
import com.lw.extraceim.ui.activity.tp.PackageEditActivity;
import com.lw.extraceim.ui.activity.tp.UnpackageInfoActivity;
import com.lw.extraceim.ui.adapter.ExtraceFragmentPagerAdapter;
import com.lw.extraceim.ui.fragments.base.BaseFragment;
import com.lw.extraceim.util.JsonUtils;
import com.lw.extraceim.zxing.ExtraceCaptureActivity;

import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class PackageFragment extends BaseFragment {
    private static final String TAG = "PackageFragment";

//    private RadioGroup radioGroup;
//    private FrameLayout frameLayout;
    private TabLayout mTabLayout;
    private ViewPager mViewPager;

    private List<BaseFragment> fragmentArrayList;
    private List<String> titleList;
    private ExtraceFragmentPagerAdapter adapter;

    public PackageFragment(Context mContext, ExtraceApplication application) {
        super(mContext, application);
    }

    @Override
    public View initView() {
        View view = View.inflate(mContext, R.layout.layout_package_fragment, null);
        mTabLayout = view.findViewById(R.id.tab_layout);
        mViewPager = view.findViewById(R.id.view_pager);

        return view;
    }


    @Override
    public void initData() {
        fragmentArrayList = new ArrayList<>();
        fragmentArrayList.add(new PackagePageFragment(mContext, application));
        fragmentArrayList.add(new UnPackingFragment(mContext, application));
        fragmentArrayList.add(new PackageListFragment(mContext, application));
        titleList = new ArrayList<>();
        titleList.add("打包");
        titleList.add("拆包");
        titleList.add("包裹列表");
        adapter = new ExtraceFragmentPagerAdapter(getChildFragmentManager(),fragmentArrayList,titleList);
        Log.d(TAG, "initData: " + adapter);
        mViewPager.setAdapter(adapter);
        mTabLayout.setupWithViewPager(mViewPager);//将TabLayout与Viewpager联动起来
//        mTabLayout.setTabsFromPagerAdapter(adapter);//给TabLayout设置适配器
    }

    //设置监听
    @Override
    public void initEvent() {
        mTabLayout.setOnTabSelectedListener(new TabLayout.BaseOnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public void refreshUI() {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case ExtraceCaptureActivity.SCAN_NEW_PACKAGE: {
                IntentResult scanResult = IntentIntegrator.parseActivityResult(IntentIntegrator.REQUEST_CODE, resultCode, data);
                if (scanResult != null) {
                    String pacakgeId = scanResult.getContents();
                    Log.d(TAG, "onActivityResult: " + pacakgeId + mContext);
                    toast("扫描成功：" + pacakgeId);
                    loadPackage(pacakgeId);
                } else {
                    Toast.makeText(getActivity(), "扫描错误", Toast.LENGTH_LONG).show();
                }
                break;
            }
            case ExtraceCaptureActivity.SCAN_UNPACK_PACKAGE: {
                //拆包回调
                IntentResult scanResult = IntentIntegrator.parseActivityResult(IntentIntegrator.REQUEST_CODE, resultCode, data);
                if (scanResult != null) {
                    String pacakgeId = scanResult.getContents();
                    Toast.makeText(getActivity(), "扫描成功：" + pacakgeId, Toast.LENGTH_LONG).show();
                    TransPackage transPackage = new TransPackage();
                    //要验证包裹编号是否合法，后期解决，现在只是简单的实现跳转
                    transPackage.setId(pacakgeId);
                    UnpackageInfoActivity.actionStart(getContext(),transPackage);
                } else {
                    Toast.makeText(getActivity(), "扫描错误", Toast.LENGTH_LONG).show();
                }
                break;
            }
        }
    }

    public void loadPackage(final String packageId) {
        PackageLoader loader = new PackageLoader(getContext());
        if (packageId != null) {
            loader.get(packageId, new HttpResponseParamCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    alert("网络错误" + e.getMessage());
                }

                @Override
                public void onResponse(HttpResponseParam response, int id) {
                    Log.d(TAG, "onResponse: " + response.reseponseJson);
                    if (response.code.getCode() == ResponseCode.SUCESS) {
                        alert("包裹信息已存在");
                    } else if (response.code.getCode() == ResponseCode.FIAL) {
                        TransPackage transPackage = new TransPackage();
                        //要验证包裹编号是否合法，后期解决，现在只是简单的实现跳转
                        transPackage.setId(packageId);
                        PackageEditActivity.actionStart(getActivity(), transPackage);
                    }
                }
            });
        }

    }
}
